// @flow
import React, { Component } from 'react'
import { Modal, Form, Input, Select, InputNumber, message } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import { limitRules } from 'store/modules/publicWebJson'
const FormItem = Form.Item
const Option = Select.Option
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
}

type Props = {
  removeNode: Function,
  refreshLimit: Function,
  form: Object,
  data: Object
}

type States = {
  visible: boolean,
  confirmLoading: boolean
}

class VarLimit extends Component {
  props: Props
  state: States
  handleSubmit: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      visible: true,
      confirmLoading: false
    }
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  handleSubmit (e) {
    e.preventDefault()
    const { form, data, removeNode } = this.props
    form.validateFields((err, fieldsValue) => {
      if (err) {
        return false
      }
      this.setState({
        confirmLoading: true
      })
      const values = {
        ...fieldsValue,
        variableId: data.id,
        monitorPointId: data.monitorPointId
      }

      universalFetch(`${__SERVICE_API__}device/templates/monitors/limits`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        body: JSON.stringify(values)
      })
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          visible: false,
          confirmLoading: false
        })
        // refreshLimit(data)
        removeNode()
        message.success('添加阈值成功')
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
        message.error('添加阈值失败')
        this.setState({
          confirmLoading: false
        })
      })
    })
  }
  render () {
    const { visible, confirmLoading } = this.state
    const { getFieldDecorator } = this.props.form
    const { removeNode } = this.props
    return (
      <Modal
        title='添加阈值'
        onCancel={removeNode}
        onOk={this.handleSubmit}
        confirmLoading={confirmLoading}
        visible={visible}
        >
        <Form>
          <FormItem
            {...formItemLayout} label='阈值名称'
            >
            {
              getFieldDecorator('name', {
                rules: [
                  { required: true, message: '请输入阈值名称', whitespace: true }
                ]
              })(
                <Input placeholder='请输入阈值名称' />
              )
            }
          </FormItem>
          <FormItem
            {...formItemLayout} label='规则'
            >
            {
              getFieldDecorator('rule', {
                rules: [
                  { required: true, message: '请选择规则', whitespace: true }
                ]
              })(
                <Select showSearch
                  filterOption={(input, option) => {
                    return option.props.children.indexOf(input) >= 0
                  }} placeholder='请选择规则'>
                  {
                    limitRules.map((d, i) => {
                      return <Option key={i} value={d.value}>{d.name}</Option>
                    })
                  }
                </Select>
              )
            }
          </FormItem>
          <FormItem
            {...formItemLayout} label='阈值大小'
            >
            {
              getFieldDecorator('value', {
                rules: [
                  { required: true, message: '请输入阈值' }
                ]
              })(
                <InputNumber placeholder='请输入阈值,仅限数字' style={{ width: '100%' }} />
              )
            }
          </FormItem>
        </Form>
      </Modal>
    )
  }
}

export default Form.create()(VarLimit)
